home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / Javacup / IN4WJCXU.TAR / internet / IN4WJCXU / other / irc / common / support.c < prev   
Encoding:
C/C++ Source or Header  |  1996-05-21  |  9.4 KB  |  478 lines

  1. /************************************************************************
  2.  *   IRC - Internet Relay Chat, common/support.c
  3.  *   Copyright (C) 1990, 1991 Armin Gruner
  4.  *
  5.  *   This program is free software; you can redistribute it and/or modify
  6.  *   it under the terms of the GNU General Public License as published by
  7.  *   the Free Software Foundation; either version 1, or (at your option)
  8.  *   any later version.
  9.  *
  10.  *   This program is distributed in the hope that it will be useful,
  11.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *   GNU General Public License for more details.
  14.  *
  15.  *   You should have received a copy of the GNU General Public License
  16.  *   along with this program; if not, write to the Free Software
  17.  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #ifndef lint
  21. static  char sccsid[] = "@(#)support.c    2.21 4/13/94 1990, 1991 Armin Gruner;\
  22. 1992, 1993 Darren Reed";
  23. #endif
  24.  
  25. #include "struct.h"
  26. #include "common.h"
  27. #include "sys.h"
  28.  
  29. extern    int errno; /* ...seems that errno.h doesn't define this everywhere */
  30. #ifndef    CLIENT_COMPILE
  31. extern    void    outofmemory();
  32. #endif
  33.  
  34. #ifdef NEED_STRTOKEN
  35. /*
  36. **     strtoken.c --      walk through a string of tokens, using a set
  37. **            of separators
  38. **            argv 9/90
  39. **
  40. **    $Id: support.c,v 6.1 1991/07/04 21:04:01 gruner stable gruner $
  41. */
  42.  
  43. char *strtoken(save, str, fs)
  44. char **save;
  45. char *str, *fs;
  46. {
  47.     char *pos = *save;    /* keep last position across calls */
  48.     Reg1 char *tmp;
  49.  
  50.     if (str)
  51.     pos = str;        /* new string scan */
  52.  
  53.     while (pos && *pos && index(fs, *pos) != NULL)
  54.     pos++;              /* skip leading separators */
  55.  
  56.     if (!pos || !*pos)
  57.     return (pos = *save = NULL);     /* string contains only sep's */
  58.  
  59.     tmp = pos;             /* now, keep position of the token */
  60.  
  61.     while (*pos && index(fs, *pos) == NULL)
  62.     pos++;             /* skip content of the token */
  63.  
  64.     if (*pos)
  65.     *pos++ = '\0';        /* remove first sep after the token */
  66.     else
  67.     pos = NULL;        /* end of string */
  68.  
  69.     *save = pos;
  70.     return(tmp);
  71. }
  72. #endif /* NEED_STRTOKEN */
  73.  
  74. #ifdef    NEED_STRTOK
  75. /*
  76. ** NOT encouraged to use!
  77. */
  78.  
  79. char *strtok(str, fs)
  80. char *str, *fs;
  81. {
  82.     static char *pos;
  83.  
  84.     return strtoken(&pos, str, fs);
  85. }
  86.  
  87. #endif /* NEED_STRTOK */
  88.  
  89. #ifdef NEED_STRERROR
  90. /*
  91. **    strerror - return an appropriate system error string to a given errno
  92. **
  93. **           argv 11/90
  94. **    $Id: support.c,v 6.1 1991/07/04 21:04:01 gruner stable gruner $
  95. */
  96.  
  97. char *strerror(err_no)
  98. int err_no;
  99. {
  100.     extern    char    *sys_errlist[];     /* Sigh... hopefully on all systems */
  101.     extern    int    sys_nerr;
  102.  
  103.     static    char    buff[40];
  104.     char    *errp;
  105.  
  106.     errp = (err_no > sys_nerr ? (char *)NULL : sys_errlist[err_no]);
  107.  
  108.     if (errp == (char *)NULL)
  109.         {
  110.         errp = buff;
  111.         (void) sprintf(errp, "Unknown Error %d", err_no);
  112.         }
  113.     return errp;
  114. }
  115.  
  116. #endif /* NEED_STRERROR */
  117.  
  118. /*
  119. **    inetntoa  --    changed name to remove collision possibility and
  120. **            so behaviour is gaurunteed to take a pointer arg.
  121. **            -avalon 23/11/92
  122. **    inet_ntoa --    returned the dotted notation of a given
  123. **            internet number (some ULTRIX don't have this)
  124. **            argv 11/90).
  125. **    inet_ntoa --    its broken on some Ultrix/Dynix too. -avalon
  126. **    $Id: support.c,v 6.1 1991/07/04 21:04:01 gruner stable gruner $
  127. */
  128.  
  129. char    *inetntoa(in)
  130. char    *in;
  131. {
  132.     static    char    buf[16];
  133.     Reg1    u_char    *s = (u_char *)in;
  134.     Reg2    int    a,b,c,d;
  135.  
  136.     a = (int)*s++;
  137.     b = (int)*s++;
  138.     c = (int)*s++;
  139.     d = (int)*s++;
  140.     (void) sprintf(buf, "%d.%d.%d.%d", a,b,c,d );
  141.  
  142.     return buf;
  143. }
  144.  
  145. #ifdef NEED_INET_NETOF
  146. /*
  147. **    inet_netof --    return the net portion of an internet number
  148. **            argv 11/90
  149. **    $Id: support.c,v 6.1 1991/07/04 21:04:01 gruner stable gruner $
  150. **
  151. */
  152.  
  153. int inet_netof(in)
  154. struct in_addr in;
  155. {
  156.     int addr = in.s_net;
  157.  
  158.     if (addr & 0x80 == 0)
  159.     return ((int) in.s_net);
  160.  
  161.     if (addr & 0x40 == 0)
  162.     return ((int) in.s_net * 256 + in.s_host);
  163.  
  164.     return ((int) in.s_net * 256 + in.s_host * 256 + in.s_lh);
  165. }
  166. #endif /* NEED_INET_NETOF */
  167.  
  168.  
  169. #if defined(DEBUGMODE) && !defined(CLIENT_COMPILE)
  170. void    dumpcore(msg, p1, p2, p3, p4, p5, p6, p7, p8, p9)
  171. char    *msg, *p1, *p2, *p3, *p4, *p5, *p6, *p7, *p8, *p9;
  172. {
  173.     static    time_t    lastd = 0;
  174.     static    int    dumps = 0;
  175.     char    corename[12];
  176.     time_t    now;
  177.     int    p;
  178.  
  179.     now = time(NULL);
  180.  
  181.     if (!lastd)
  182.         lastd = now;
  183.     else if (now - lastd < 60 && dumps > 2)
  184.         (void)s_die();
  185.     if (now - lastd > 60)
  186.         {
  187.         lastd = now;
  188.         dumps = 1;
  189.         }
  190.     else
  191.         dumps++;
  192.     p = getpid();
  193.     if (fork()>0) {
  194.         kill(p, 3);
  195.         kill(p, 9);
  196.     }
  197.     write_pidfile();
  198.     (void)sprintf(corename, "core.%d", p);
  199.     (void)rename("core", corename);
  200.     Debug((DEBUG_FATAL, "Dumped core : core.%d", p));
  201.     sendto_ops("Dumped core : core.%d", p);
  202.     Debug((DEBUG_FATAL, msg, p1, p2, p3, p4, p5, p6, p7, p8, p9));
  203.     sendto_ops(msg, p1, p2, p3, p4, p5, p6, p7, p8, p9);
  204.         (void)s_die();
  205. }
  206.  
  207. static    char    *marray[20000];
  208. static    int    mindex = 0;
  209.  
  210. #define    SZ_EX    (sizeof(char *) + sizeof(size_t) + 4)
  211. #define    SZ_CHST    (sizeof(char *) + sizeof(size_t))
  212. #define    SZ_CH    (sizeof(char *))
  213. #define    SZ_ST    (sizeof(size_t))
  214.  
  215. char    *MyMalloc(x)
  216. size_t    x;
  217. {
  218.     register int    i;
  219.     register char    **s;
  220.     char    *ret;
  221.  
  222.     ret = (char *)malloc(x + (size_t)SZ_EX);
  223.  
  224.     if (!ret)
  225.         {
  226. # ifndef    CLIENT_COMPILE
  227.         outofmemory();
  228. # else
  229.         perror("malloc");
  230.         exit(-1);
  231. # endif
  232.         }
  233.     bzero(ret, (int)x + SZ_EX);
  234.     bcopy((char *)&ret, ret, SZ_CH);
  235.     bcopy((char *)&x, ret + SZ_ST, SZ_ST);
  236.     bcopy("VAVA", ret + SZ_CHST + (int)x, 4);
  237.     Debug((DEBUG_MALLOC, "MyMalloc(%ld) = %#x", x, ret+8));
  238.     for(i = 0, s = marray; *s && i < mindex; i++, s++)
  239.         ;
  240.      if (i < 20000)
  241.         {
  242.         *s = ret;
  243.         if (i == mindex)
  244.             mindex++;
  245.         }
  246.     return ret + SZ_CHST;
  247.     }
  248.  
  249. char    *MyRealloc(x, y)
  250. char    *x;
  251. size_t    y;
  252.     {
  253.     register int    l;
  254.     register char    **s;
  255.     char    *ret, *cp;
  256.     size_t    i;
  257.     int    k;
  258.  
  259.     x -= SZ_CHST;
  260.     bcopy(x, (char *)&cp, SZ_CH);
  261.     bcopy(x + SZ_CH, (char *)&i, SZ_ST);
  262.     bcopy(x + (int)i + SZ_CHST, (char *)&k, 4);
  263.     if (bcmp((char *)&k, "VAVA", 4) || (x != cp))
  264.         dumpcore("MyRealloc %#x %d %d %#x %#x", x, y, i, cp, k);
  265.     ret = (char *)realloc(x, y + (size_t)SZ_EX);
  266.  
  267.     if (!ret)
  268.         {
  269. # ifndef    CLIENT_COMPILE
  270.         outofmemory();
  271. # else
  272.         perror("realloc");
  273.         exit(-1);
  274. # endif
  275.         }
  276.     bcopy((char *)&ret, ret, SZ_CH);
  277.     bcopy((char *)&y, ret + SZ_CH, SZ_ST);
  278.     bcopy("VAVA", ret + SZ_CHST + (int)y, 4);
  279.     Debug((DEBUG_NOTICE, "MyRealloc(%#x,%ld) = %#x", x, y, ret + SZ_CHST));
  280.     for(l = 0, s = marray; *s != x && l < mindex; l++, s++)
  281.         ;
  282.      if (l < mindex)
  283.         *s = NULL;
  284.     else if (l == mindex)
  285.         Debug((DEBUG_MALLOC, "%#x !found", x));
  286.     for(l = 0, s = marray; *s && l < mindex; l++,s++)
  287.         ;
  288.      if (l < 20000)
  289.         {
  290.         *s = ret;
  291.         if (l == mindex)
  292.             mindex++;
  293.         }
  294.     return ret + SZ_CHST;
  295.     }
  296.  
  297. void    MyFree(x)
  298. char    *x;
  299. {
  300.     size_t    i;
  301.     char    *j;
  302.     u_char    k[4];
  303.     register int    l;
  304.     register char    **s;
  305.  
  306.     if (!x)
  307.         return;
  308.     x -= SZ_CHST;
  309.  
  310.     bcopy(x, (char *)&j, SZ_CH);
  311.     bcopy(x + SZ_CH, (char *)&i, SZ_ST);
  312.     bcopy(x + SZ_CHST + (int)i, (char *)k, 4);
  313.  
  314.     if (bcmp((char *)k, "VAVA", 4) || (j != x))
  315.         dumpcore("MyFree %#x %ld %#x %#x", x, i, j,
  316.              (k[3]<<24) | (k[2]<<16) | (k[1]<<8) | k[0]);
  317.  
  318. #undef    free
  319.     (void)free(x);
  320. #define    free(x)    MyFree(x)
  321.     Debug((DEBUG_MALLOC, "MyFree(%#x)",x + SZ_CHST));
  322.  
  323.     for (l = 0, s = marray; *s != x && l < mindex; l++, s++)
  324.         ;
  325.     if (l < mindex)
  326.         *s = NULL;
  327.     else if (l == mindex)
  328.         Debug((DEBUG_MALLOC, "%#x !found", x));
  329. }
  330.  
  331. #else
  332. char    *MyMalloc(x)
  333. size_t    x;
  334. {
  335.     char *ret = (char *)malloc(x);
  336.  
  337.     if (!ret)
  338.         {
  339. # ifndef    CLIENT_COMPILE
  340.         outofmemory();
  341. # else
  342.         perror("malloc");
  343.         exit(-1);
  344. # endif
  345.         }
  346.     return    ret;
  347. }
  348.  
  349. char    *MyRealloc(x, y)
  350. char    *x;
  351. size_t    y;
  352.     {
  353.     char *ret = (char *)realloc(x, y);
  354.  
  355.     if (!ret)
  356.         {
  357. # ifndef CLIENT_COMPILE
  358.         outofmemory();
  359. # else
  360.         perror("realloc");
  361.         exit(-1);
  362. # endif
  363.         }
  364.     return ret;
  365.     }
  366. #endif
  367.  
  368.  
  369. /*
  370. ** read a string terminated by \r or \n in from a fd
  371. **
  372. ** Created: Sat Dec 12 06:29:58 EST 1992 by avalon
  373. ** Returns:
  374. **    0 - EOF
  375. **    -1 - error on read
  376. **     >0 - number of bytes returned (<=num)
  377. ** After opening a fd, it is necessary to init dgets() by calling it as
  378. **    dgets(x,y,0);
  379. ** to mark the buffer as being empty.
  380. */
  381. int    dgets(fd, buf, num)
  382. int    fd, num;
  383. char    *buf;
  384. {
  385.     static    char    dgbuf[8192];
  386.     static    char    *head = dgbuf, *tail = dgbuf;
  387.     register char    *s, *t;
  388.     register int    n, nr;
  389.  
  390.     /*
  391.     ** Sanity checks.
  392.     */
  393.     if (head == tail)
  394.         *head = '\0';
  395.     if (!num)
  396.         {
  397.         head = tail = dgbuf;
  398.         *head = '\0';
  399.         return 0;
  400.         }
  401.     if (num > sizeof(dgbuf) - 1)
  402.         num = sizeof(dgbuf) - 1;
  403. dgetsagain:
  404.     if (head > dgbuf)
  405.         {
  406.         for (nr = tail - head, s = head, t = dgbuf; nr > 0; nr--)
  407.             *t++ = *s++;
  408.         tail = t;
  409.         head = dgbuf;
  410.         }
  411.     /*
  412.     ** check input buffer for EOL and if present return string.
  413.     */
  414.     if (head < tail &&
  415.         ((s = index(head, '\n')) || (s = index(head, '\r'))) && s < tail)
  416.         {
  417.         n = MIN(s - head + 1, num);    /* at least 1 byte */
  418. dgetsreturnbuf:
  419.         bcopy(head, buf, n);
  420.         head += n;
  421.         if (head == tail)
  422.             head = tail = dgbuf;
  423.         return n;
  424.         }
  425.  
  426.     if (tail - head >= num)        /* dgets buf is big enough */
  427.         {
  428.         n = num;
  429.         goto dgetsreturnbuf;
  430.         }
  431.  
  432.     n = sizeof(dgbuf) - (tail - dgbuf) - 1;
  433.     nr = read(fd, tail, n);
  434.     if (nr == -1)
  435.         {
  436.         head = tail = dgbuf;
  437.         return -1;
  438.         }
  439.     if (!nr)
  440.         {
  441.         if (tail > head)
  442.             {
  443.             n = MIN(tail - head, num);
  444.             goto dgetsreturnbuf;
  445.             }
  446.         head = tail = dgbuf;
  447.         return 0;
  448.         }
  449.     tail += nr;
  450.     *tail = '\0';
  451.     for (t = head; (s = index(t, '\n')); )
  452.         {
  453.         if ((s > head) && (s > dgbuf))
  454.             {
  455.             t = s-1;
  456.             for (nr = 0; *t == '\\'; nr++)
  457.                 t--;
  458.             if (nr & 1)
  459.                 {
  460.                 t = s+1;
  461.                 s--;
  462.                 nr = tail - t;
  463.                 while (nr--)
  464.                     *s++ = *t++;
  465.                 tail -= 2;
  466.                 *tail = '\0';
  467.                 }
  468.             else
  469.                 s++;
  470.             }
  471.         else
  472.             s++;
  473.         t = s;
  474.         }
  475.     *tail = '\0';
  476.     goto dgetsagain;
  477. }
  478.